package nus.iss.laps.general;
import java.io.File;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;

import nus.iss.laps.model.Leave;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExportXls {

	@Autowired
	public void export(ArrayList<Leave> leaveList) {
		

		try {
			// This data needs to be written (Object[])
			Map<String, Object[]> data = new TreeMap<String, Object[]>();
			data.put("1", new Object[] 
			{ "Leave ID", "Created Date", "Modified Date","Leave Status","Employee ID",
			"Start Date","End Date","Dissemination","Reason","Days","Supervisor ID","Comment"});
			
			WritableWorkbook workBook = Workbook.createWorkbook(new File("emp.xls"));
			WritableSheet sheet = workBook.createSheet("Sheet1", 0);
			Label l_leaveID = new Label(0, 0, "Leave ID");
			Label l_createdDate = new Label(1, 0, "Created Date");
			Label l_modifiedDate = new Label(2, 0, "Modified Date");
			Label l_leaveStatus = new Label(3, 0, "Leave Status");
			Label l_employeeID = new Label(4, 0, "Employee ID");
			Label l_startDate = new Label(5, 0, "Start Date");
			Label l_endDate = new Label(6, 0, "End Date");
			Label l_dissemination = new Label(7, 0, "Dissemination");
			Label l_reason= new Label(8, 0, "Reason");
			Label l_days = new Label(9, 0, "Days");
			Label l_supervisorID = new Label(10, 0, "Supervisor ID");
			Label l_comment = new Label(11, 0, "Comment");
			
			
			sheet.addCell(l_leaveID);
			sheet.addCell(l_createdDate);
			sheet.addCell(l_modifiedDate);
			sheet.addCell(l_leaveStatus);
			sheet.addCell(l_employeeID);
			sheet.addCell(l_startDate);
			sheet.addCell(l_endDate);
			sheet.addCell(l_dissemination);
			sheet.addCell(l_reason);
			sheet.addCell(l_days);
			sheet.addCell(l_supervisorID);
			sheet.addCell(l_comment);
			
			int r = 1;
			int c = 0;
			for (Leave leave : leaveList) {
				sheet.addCell(new Label(c++, r, String.valueOf(leave.getLeaveID())));
				sheet.addCell(new Label(c++, r, LapsUtil.stringToFormattedDate(leave.getCreatedDate()))); // created date
				sheet.addCell(new Label(c++, r, LapsUtil.stringToFormattedDate(leave.getModifiedDate())));
				sheet.addCell(new Label(c++, r, String.valueOf(leave.getLeaveStatus())));
				sheet.addCell(new Label(c++, r, leave.getEmployeeID()));
				sheet.addCell(new Label(c++, r, LapsUtil.stringToFormattedDate(leave.getStartDate())));
				sheet.addCell(new Label(c++, r, LapsUtil.stringToFormattedDate(leave.getEndDate())));
				sheet.addCell(new Label(c++, r, leave.getDissemination()));
				sheet.addCell(new Label(c++,r,leave.getReason()));
				sheet.addCell(new Label(c++,r,String.valueOf(leave.getDays())));
				sheet.addCell(new Label(c++,r,String.valueOf(leave.getSupervisorID())));
				sheet.addCell(new Label(c++,r,String.valueOf(leave.getComment())));
				c=0;
				r++;
			}
			
			workBook.write();
			workBook.close();
			

			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}
